{
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# Tensorflow Basic"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "import tensorflow as tf"
      ],
      "outputs": [],
      "execution_count": 14,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false
      }
    },
    {
      "cell_type": "code",
      "source": [
        "x = tf.constant(2)\n",
        "y = tf.constant(4)"
      ],
      "outputs": [],
      "execution_count": 2,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false
      }
    },
    {
      "cell_type": "code",
      "source": [
        "with tf.Session() as sess:\n",
        "    print(\"x: %i\" % sess.run(x), \"y: %i\" % sess.run(y))\n",
        "    print(\"Addition with constants: %i\" % sess.run(x+y))\n",
        "    print(\"Multiplication with constants: %i\" % sess.run(x*y))"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "x: 2 y: 4\n",
            "Addition with constants: 6\n",
            "Multiplication with constants: 8\n"
          ]
        }
      ],
      "execution_count": 3,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false
      }
    },
    {
      "cell_type": "code",
      "source": [
        "add = tf.add(x, y)\n",
        "sess = tf.Session()\n",
        "value_of_add = sess.run(add)\n",
        "print(value_of_add)\n",
        "sess.close()"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "6\n"
          ]
        }
      ],
      "execution_count": 4,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false
      }
    },
    {
      "cell_type": "code",
      "source": [
        "sub = tf.subtract(x, y)\n",
        "sess = tf.Session()\n",
        "value_of_sub = sess.run(sub)\n",
        "print(value_of_sub)\n",
        "sess.close()"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "-2\n"
          ]
        }
      ],
      "execution_count": 5,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false
      }
    },
    {
      "cell_type": "code",
      "source": [
        "mult = tf.multiply(x, y)\n",
        "sess = tf.Session()\n",
        "value_of_mult = sess.run(mult)\n",
        "print(value_of_mult)\n",
        "sess.close()"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "8\n"
          ]
        }
      ],
      "execution_count": 6,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false
      }
    },
    {
      "cell_type": "code",
      "source": [
        "div = tf.divide(x, y)\n",
        "sess = tf.Session()\n",
        "value_of_div = sess.run(div)\n",
        "print(value_of_div)\n",
        "sess.close()"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "0.5\n"
          ]
        }
      ],
      "execution_count": 7,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# y = W.x + b\n",
        "x = tf.constant(-2.0, name=\"x\", dtype=tf.float32)\n",
        "W = tf.constant(8.0, name=\"W\", dtype=tf.float32)\n",
        "b = tf.constant(10.0, name=\"b\", dtype=tf.float32)\n",
        "\n",
        "y = tf.Variable(tf.add(tf.multiply(W, x), b))\n",
        "\n",
        "init = tf.global_variables_initializer()\n",
        "\n",
        "with tf.Session() as session:\n",
        "    session.run(init)\n",
        "    print(session.run(y))   "
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "-6.0\n"
          ]
        }
      ],
      "execution_count": 8,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false
      }
    },
    {
      "cell_type": "code",
      "source": [
        "a = tf.constant(2.8)\n",
        "b = tf.constant(4.3)\n",
        "\n",
        "# Basic Math\n",
        "total = a + b\n",
        "diff = a - b\n",
        "quot = tf.div(a, b)\n",
        "prod = tf.multiply(a, b)\n",
        "\n",
        "# Matrix Multiplication\n",
        "c = tf.constant([[1,2], [3,4], [5,6]])\n",
        "d = tf.constant([[9,8,7], [6,5,4]])\n",
        "matrix_prod = tf.matmul(c, d)\n",
        "\n",
        "# Excute the Session\n",
        "with tf.Session() as sess:\n",
        "    print(\"Sum: %f\" % sess.run(total))\n",
        "    print(\"Difference: %f\" % sess.run(diff))\n",
        "    print(\"Division: %f\" % sess.run(quot))\n",
        "    print(\"Multiplication: %f\" % sess.run(prod))\n",
        "    print(\"Matrix prouct: \",  sess.run(matrix_prod))\n",
        "    print(\"Round: %f\" % sess.run(tf.round(a)))\n",
        "    print(\"Round: %f\" % sess.run(tf.round(b)))"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Sum: 7.100000\n",
            "Difference: -1.500000\n",
            "Division: 0.651163\n",
            "Multiplication: 12.040000\n",
            "Matrix prouct:  [[21 18 15]\n",
            " [51 44 37]\n",
            " [81 70 59]]\n",
            "Round: 3.000000\n",
            "Round: 4.000000\n"
          ]
        }
      ],
      "execution_count": 9,
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "# 2-D tensor\n",
        "a = tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3])\n",
        "\n",
        "with tf.Session() as sess:\n",
        "    print(\"2-D tensor:\")\n",
        "    print(sess.run(a))"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "2-D tensor:\n",
            "[[1 2 3]\n",
            " [4 5 6]]\n"
          ]
        }
      ],
      "execution_count": 10,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# 2-D tensor\n",
        "b = tf.constant([7, 8, 9, 10, 11, 12], shape=[3, 2])\n",
        "\n",
        "with tf.Session() as sess:\n",
        "    print(\"2-D tensor:\")\n",
        "    print(sess.run(b))"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "2-D tensor:\n",
            "[[ 7  8]\n",
            " [ 9 10]\n",
            " [11 12]]\n"
          ]
        }
      ],
      "execution_count": 11,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false
      }
    },
    {
      "cell_type": "code",
      "source": [
        "c = tf.matmul(a, b)\n",
        "\n",
        "with tf.Session() as sess:\n",
        "    print(\"a * b:\")\n",
        "    print(sess.run(c))"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "a * b:\n",
            "[[ 58  64]\n",
            " [139 154]]\n"
          ]
        }
      ],
      "execution_count": 12,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# 3-D tensor\n",
        "import numpy as np\n",
        "x = tf.constant(np.arange(1, 13, dtype=np.int32),\n",
        "                shape=[2, 2, 3])\n",
        "\n",
        "y = tf.constant(np.arange(13, 25, dtype=np.int32),\n",
        "                shape=[2, 3, 2])\n",
        "\n",
        "z = tf.constant(np.arange(1, 13, dtype=np.int32),\n",
        "                shape=[2, 2, 3])\n",
        "\n",
        "with tf.Session() as sess:\n",
        "    print(\"3-D tensor:\")\n",
        "    print('--------------------')\n",
        "    print(sess.run(x))\n",
        "    print('--------------------')\n",
        "    print(sess.run(y))\n",
        "    print('--------------------')\n",
        "    print('3-D Multiplication:')\n",
        "    print(sess.run(tf.matmul(x,y)))\n",
        "    print('--------------------')\n",
        "    print('Dot Product:')\n",
        "    print(sess.run(tf.tensordot(x,z, 3)))"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "3-D tensor:\n",
            "--------------------\n",
            "[[[ 1  2  3]\n",
            "  [ 4  5  6]]\n",
            "\n",
            " [[ 7  8  9]\n",
            "  [10 11 12]]]\n",
            "--------------------\n",
            "[[[13 14]\n",
            "  [15 16]\n",
            "  [17 18]]\n",
            "\n",
            " [[19 20]\n",
            "  [21 22]\n",
            "  [23 24]]]\n",
            "--------------------\n",
            "3-D Multiplication:\n",
            "[[[ 94 100]\n",
            "  [229 244]]\n",
            "\n",
            " [[508 532]\n",
            "  [697 730]]]\n",
            "--------------------\n",
            "Dot Product:\n",
            "650\n"
          ]
        }
      ],
      "execution_count": 13,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false
      }
    }
  ],
  "metadata": {
    "kernel_info": {
      "name": "python3"
    },
    "language_info": {
      "pygments_lexer": "ipython3",
      "nbconvert_exporter": "python",
      "codemirror_mode": {
        "version": 3,
        "name": "ipython"
      },
      "version": "3.5.5",
      "name": "python",
      "file_extension": ".py",
      "mimetype": "text/x-python"
    },
    "kernelspec": {
      "name": "python3",
      "language": "python",
      "display_name": "Python 3"
    },
    "nteract": {
      "version": "0.15.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 4
}